//
//  InforViewController.m
//  TenThousand
//
//  Created by baixue on 16/7/31.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "InforViewController.h"
#import "InforModel.h"
#import "InformationCell.h"
#import "MeterialsCell.h"
#import "SecurityCell.h"


@interface InforViewController ()<UITableViewDataSource,UITableViewDelegate>

{
    InforModel *_model;
    NSMutableDictionary *_inforDic;
}
@property(nonatomic,strong) UITableView *myTableView;
@property(nonatomic, strong) NSMutableArray *imageArrM;

@end
#define TitleArr @[@"用户信息",@"图文信息",@"风控保障"]
@implementation InforViewController

- (NSMutableArray *)imageArrM {
    if (!_imageArrM) {
        _imageArrM = [NSMutableArray array];
    }
    return _imageArrM;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.myTableView];
    [self getBorrowDetailNew];
}





-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height-64-40) style:UITableViewStyleGrouped];
    }
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.backgroundColor = [UIColor whiteColor];
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _myTableView.showsVerticalScrollIndicator = NO;
    return _myTableView;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return _model.ImageItemList.count;
    }
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section == 0) {
        InformationCell *informationCell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!informationCell) {
            informationCell = [[[NSBundle mainBundle] loadNibNamed:@"InformationCell" owner:self options:nil] firstObject];
        }
        if (_model) {
            NSString *userName = [NSString stringWithFormat:@"%@", _model.BLoginId];
            NSRange r = {3,6};
            userName = [userName stringByReplacingCharactersInRange:r withString:@"******"];
            informationCell.userName.text = [NSString stringWithFormat:@"用户名：%@", userName];
            [informationCell setDataWithModel:_model];
            [informationCell cellAutoLayoutHeight:_model.Introduction];
        }
        informationCell.selectionStyle =  UITableViewCellSelectionStyleNone;
        
        
        return informationCell;
    } else if (indexPath.section == 1){
        MeterialsCell *meterialsCell = [tableView dequeueReusableCellWithIdentifier:@"MeterialsCell"];
        if (!meterialsCell) {
            meterialsCell = [[[NSBundle mainBundle] loadNibNamed:@"MeterialsCell" owner:self options:nil] firstObject];
        }
        ImageItemList *model = _model.ImageItemList[indexPath.row];
        if (model) {
            [meterialsCell cellAutoLayoutHeight:model.Picsmall];
            
        }
        
        return meterialsCell;
    }else{
        SecurityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"SecurityCell" owner:self options:nil] firstObject];
        }
        
        [cell cellAutoLayoutHeight:_model.Riskcontrol];
        return cell;
        
        
    }
}


//返回行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        InformationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InformationCell"];
        if (!cell) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"InformationCell" owner:self options:nil] lastObject];
        }
        if (_model) {
            [cell cellAutoLayoutHeight:_model.Introduction];
        }
        CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];
        return size.height;
    }
    if (indexPath.section == 2) {
        SecurityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"SecurityCell" owner:self options:nil] firstObject];
        }
        if (_model) {
            [cell cellAutoLayoutHeight:_model.Riskcontrol];
        }
        CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];
        return size.height;
    }
    else {
        
//        MeterialsCell *meterialsCell = [tableView dequeueReusableCellWithIdentifier:@"MeterialsCell"];
//        if (!meterialsCell) {
//            meterialsCell = [[[NSBundle mainBundle] loadNibNamed:@"MeterialsCell" owner:self options:nil] firstObject];
//        }
//        
//        ImageItemList *model = _model.ImageItemList[indexPath.row];
//        if (model) {
//            [meterialsCell cellAutoLayoutHeight:model.Picsmall];
//            
//        }
//        CGSize size = [meterialsCell.contentView systemLayoutSizeFittingSize:UILayoutFittingExpandedSize];
//        return size.height;
        return 280 * [[UIScreen mainScreen] bounds].size.height / 736;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_Width, 40)];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 40)];
    lable.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:1];
    lable.text = [NSString stringWithFormat:@"    %@",TitleArr[section]];
    [header addSubview:lable];
    
    return header;
}

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}




//项目详情
//Borrow.GetBorrowDetailNew
-(void)getBorrowDetailNew{
    NSDictionary *para = @{
                           @"BorrowId":self.Borrowid,
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Borrow.GetBorrowDetailNew" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",object);
    
        NSMutableDictionary *inforDic = [NSMutableDictionary dictionary];
        if ([[object[@"Data"] firstObject][@"BorrowItemList"] count]) {
            [inforDic addEntriesFromDictionary:[object[@"Data"] firstObject][@"BorrowItemList"][0]];
        }
        if ([[object[@"Data"] firstObject] count]) {
            [inforDic addEntriesFromDictionary:[object[@"Data"] firstObject]];
        }
        if ([[object[@"Data"] firstObject][@"UserItemList"] count]) {
            [inforDic addEntriesFromDictionary:[object[@"Data"] firstObject][@"UserItemList"][0]];
        }
        
        _model = [InforModel mj_objectWithKeyValues:inforDic];
        self.navigationItem.title = [NSString stringWithFormat:@"%@%@", _model.ProjectName, _model.Code];
        [self.myTableView reloadData];
    } withFailureBlock:^(NSError *error) {
        NSLog(@"%@",error);
        [ToastView showErrorWithStatus:@"网络请求失败"];
    } progress:nil];
}





@end
